<?php
/**
 * @file $HeadURL: bbsindexcontroller.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-4-10 8:14:18 $
 * @brief 
 */
namespace haibao\cms\business\control\pc\index;

use \haibao\cms\model\filter\tryproduct as tryproductFilter; 
use \haibao\cms\model\filter\ImageLibrary as ImageLibraryFilter;
use \haibao\cms\business\TryProduct as TryProductBusiness;
use \haibao\frame\data\query\Condition;
use \haibao\cms\data\control\PublicController as PublicControllerData;
use \haibao\cms\business\control\ArticleController as ArticleController;

class ForumIndexController extends \haibao\cms\business\control\pc\channel\ChannelController{
	const BBS_ATTACTHMENT_FRONT = 'data/attachment/forum/';
	public function __construct($updateTime = null, $controlId, $isPublish){
		parent::__construct($updateTime, $controlId, $isPublish);
	}
	
	public function displayTemplateHtml(){
		
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);	
		$path = $this->generateHtmlPath(__CLASS__);
		
		$pkThreadData = array();
		$tryCenterData = array();
		
		$tryCenterArray = Array();
		$webSitePlateArray = Array();
		if($controlData['manual'] && $controlData['manual']['pkThread']){
			$pkThreadData = $this->getPkThreadData($controlData['manual']['pkThread']);
		}
		$this->assign('pkThreadData', $pkThreadData);
		
		if($controlData['manual'] && $controlData['manual']['tryCenter']){
			$tryCenterData = $this->getTryCenterData($controlData['manual']['tryCenter']);
			foreach($tryCenterData as $key => $value){
				$tryCenterArray[$key]['Title'] = $value->Title;
				$tryCenterArray[$key]['PicId'] = $value->PicId;
				$tryCenterArray[$key]['EndDay'] = round((strtotime($value->ApplyEndTime->format('Y-m-d H:i:s')) - time())/86400);
				$tryCenterArray[$key]['PicUrl'] = $this->replaceImageLink($value->PicUrl->Filename,146,146);
				$tryCenterArray[$key]['Price'] = $value->Price;
				$tryCenterArray[$key]['Amount'] = $value->Amount;
				$tryCenterArray[$key]['Link'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_TRY).'/try/'.$value->Id.'/';
			}			
		}	
		$this->assign('tryUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_TRY));	
		$this->assign('tryCenterData', $tryCenterArray);
		
		if($controlData['manual'] && $controlData['manual']['tryCenter']){
			$articlePlateArray = array();		
			$image = "";
						
			$webSitePlate = $this->getWebSitePlate();
			if($webSitePlate){
				foreach($webSitePlate as $key => $value){
					if(isset($value['tid'])){
						if($value['hasProPic'] == 1){
							if($value['fromWx']==2 ){
								$value['image'] = parent::getImageLink($value['image'],150,200,100,0);
							}else{
								$value['image'] = self::BBS_ATTACTHMENT_FRONT.$value['image'];
							}
						}
						$webSitePlateArray[] = $value;
					}	
				}				
			}	
		}		
		$this->assign('webSitePlate', $webSitePlateArray);	

		$today = date('Y.m.d');
		$this->assign('today', $today);
		
		$dayNum = array();
		for($i=1;$i<=7;$i++){
			$dayNum[$i]['day'] = $i;
			if($i==1 || $i==2){
				$dayNum[$i]['credit'] = 5;
			}elseif($i==3 || $i==4){
				$dayNum[$i]['credit'] = 10;
			}elseif($i==5){
				$dayNum[$i]['credit'] = 15;
			}elseif($i==6){
				$dayNum[$i]['credit'] = 25;
			}elseif($i==7){
				$dayNum[$i]['credit'] = 30;
			}
		}
		$this->assign('dayNum', $dayNum);
		
		$this->generateHtml($path,$content);	
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	private function getPkThreadData($limit = 1){
		$publicControllerData = new PublicControllerData;		
		return $publicControllerData->getPkThreadData($limit);
	}
	
	private function getTryCenterData($limit = 3){
		$tryProductFilter = new tryproductFilter;
		$imageLibraryFilter = new ImageLibraryFilter;
		$tryProductFilter->select(array(
				tryproductFilter::CONDITION_FIELD_NAME_ID,
				tryproductFilter::CONDITION_FIELD_NAME_TITLE,
				tryproductFilter::CONDITION_FIELD_NAME_PRICE,
				tryproductFilter::CONDITION_FIELD_NAME_PIC_ID,			
				tryproductFilter::CONDITION_FIELD_NAME_APPLY_START_TIME,
				tryproductFilter::CONDITION_FIELD_NAME_APPLY_END_TIME,
				tryproductFilter::CONDITION_FIELD_NAME_AMOUNT
		));
		$imageLibraryFilter->select(array(
				ImageLibraryFilter::CONDITION_FIELD_NAME_ID,
				ImageLibraryFilter::CONDITION_FIELD_NAME_PICID,
				ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME
		));
		$tryProductFilter->leftJoin($imageLibraryFilter,null,tryproductFilter::CONDITION_FIELD_NAME_PIC_ID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,tryproductFilter::CONDITION_FIELD_NAME_PIC_URL,false);
		$today = date('Y-m-d H:i:s');
		$tryProductFilter->where(tryproductFilter::CONDITION_FIELD_NAME_APPLY_START_TIME,Condition::CONDITION_LESSEQUAL,$today);
		$tryProductFilter->where(tryproductFilter::CONDITION_FIELD_NAME_APPLY_END_TIME, Condition::CONDITION_GREATEREQUAL, $today);
		$tryProductFilter->where(tryproductFilter::CONDITION_FIELD_NAME_PUBLISHED, Condition::CONDITION_EQUAL, 1);
		$tryProductFilter->where(tryproductFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, 1);
		$tryProductFilter->order(tryproductFilter::CONDITION_FIELD_NAME_ID,false);
		$tryProductFilter->limit(0,$limit);
		return TryProductBusiness::getTryProductByQuery($tryProductFilter);
	}
	
	private function getWebSitePlate(){
		$publicControllerData = new PublicControllerData;
		return $publicControllerData->getWebSitePlate();
	}
	
	private function getArticlePlate(){
		$publicControllerData = new PublicControllerData;
		return $publicControllerData->getArticlePlate();		
	}
	
}